const {
    getById,  // 根据id获取试卷
    getList,  // 获取试卷列表
    create,   // 创建试卷
    edit,     // 编辑试卷
    remove,   // 删除试卷
} = require("../../service/examination/testpaper")

exports.testpaperApi = async ctx => {
    // 获取url 字段 继而判断接下来操作
    let url = ctx.request.url.split("/");
    let urlKey = url[2].split("?")[0]
    const { id } = ctx.params || "";
    const { pageNum, pageSize } = ctx.query || ""
    const { title, creator, updator, choice, answer, judge, conpletion, starttime, endtime, choicebranch, answerbranch, conpletionbranch, judgebranch } = ctx.request.body || "";
    let res;
    switch (ctx.request.method) {
        case "GET": if (urlKey == "byid") {
            res = await getById({ id })
        } else {
            if (urlKey == "list") {
                res = await getList({ pageNum, pageSize })
            }
        }; break;
        case "POST": res = await create({ title, creator, choice, answer, judge, conpletion, starttime, endtime, choicebranch, answerbranch, conpletionbranch, judgebranch }); break;
        case "DELETE": res = await remove({ id }); break;
        case "PUT": res = await edit({ id, title, updator, choice, answer, judge, conpletion, starttime, endtime, choicebranch, answerbranch, conpletionbranch, judgebranch }); break;
        default: res = { msg: "操作失败 请求方法不规范", code: "400" }
    }
    ctx.body = res
}